package org.stvd.service.openim.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stvd.common.SecurityUserHolder;
import org.stvd.common.utils.ServiceResult;
import org.stvd.common.utils.StringsUtil;
import org.stvd.common.utils.openim.OpenImApi;
import org.stvd.entities.admin.UserBase;
import org.stvd.entities.admin.UserLogin;
import org.stvd.repository.admin.UserBaseDao;
import org.stvd.service.admin.UsersService;
import org.stvd.service.base.impl.BaseServiceImpl;
import org.stvd.service.openim.OpenimUserService;

import com.alibaba.fastjson.JSONObject;

import net.sf.json.JSONArray;

@Service("OpenimUserService")
public class OpenimUserServiceImpl extends BaseServiceImpl<Object> implements OpenimUserService {

    @Autowired
    private UsersService usersService;
    @Resource(name = "UserBaseDao")
    private UserBaseDao userBaseDao;

    @Override
    public ServiceResult<Map<String, Object>> initOpenimUser() {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String,Object>>();
        UserBase userBase = userBaseDao.findByPk(UserBase.class, SecurityUserHolder.getCurrentUserid());
        if (userBase == null) {
            serviceResult.setDataError("获取用户信息失败!");
            return serviceResult;
        }
        List<UserLogin> listUserLoginByUserId = usersService.findUserLoginByUserId(SecurityUserHolder.getCurrentUserid());
        String openImUserId = listUserLoginByUserId.get(0).getLoginAccount();
        String checkUserRegisterRes = OpenImApi.getInstance().checkUserRegister(openImUserId);
        if (StringsUtil.isEmpty(checkUserRegisterRes)) {
            serviceResult.setDataError("检查openim用户请求失败！");
            return serviceResult;
        }
        JSONObject jsonObject = JSONObject.parseObject(checkUserRegisterRes);
        int errCode = Integer.valueOf(jsonObject.getString("errCode"));
        if (errCode != 0) { // 注册openim失败
            serviceResult.setDataError("检查openim用户请求失败!："+jsonObject.getString("errMsg"));
            return serviceResult;
        }
        String data = jsonObject.getString("data");
        if (StringsUtil.isEmpty(data)) {
            serviceResult.setDataError("检查openim用户返回数据缺失！");
            return serviceResult;
        }
        JSONArray json = JSONArray.fromObject(data);
        String accountStatus = null;
        for(int i= 0; i < json.size(); i++){
            net.sf.json.JSONObject job = json.getJSONObject(i);
            accountStatus = job.get("accountStatus").toString();
        }
        if (accountStatus.equals("unregistered")) {//没有注册 注册账号
            String registerNewUserRes = OpenImApi.getInstance().userRegister(openImUserId, userBase.getNickName(), StringsUtil.getRandomCode(16,"2"));
            if (StringsUtil.isEmpty(registerNewUserRes)) {
                serviceResult.setDataError("注册openim请求失败！");
                return serviceResult;
            }
            JSONObject jsonRegisterObject = JSONObject.parseObject(registerNewUserRes);
            int errregisterCode = Integer.valueOf(jsonRegisterObject.getString("errCode"));
            if (errregisterCode != 0) { // 注册openim失败
                serviceResult.setDataError("openim注册用户失败!："+jsonRegisterObject.getString("errMsg"));
                return serviceResult;
            }
            String registerdata = jsonObject.getString("data");
            if (StringsUtil.isEmpty(registerdata)) {
                serviceResult.setDataError("注册openim请求返回数据缺失！");
                return serviceResult;
            }
        }
        serviceResult.setMessage("操作成功!");
        return serviceResult;
    }
}
;